package cn.itsource.controller;

import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.service.IComboService;
import cn.itsource.domain.Combo;
import cn.itsource.query.ComboQuery;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @desc
 * @author bobo
 * @since 2022-12-18
 */
@RestController
@RequestMapping("/combo")
public class ComboController {

    @Autowired
    public IComboService comboService;

    /**
     * 保存和修改操作公用此方法
     * @param combo 前端传递来的实体数据
     */
    @PostMapping(value="/save")
    public AjaxResult save(@RequestBody Combo combo){
        if(combo.getId()!=null){
                comboService.updateById(combo);
        }else{
                comboService.insert(combo);
        }
        return AjaxResult.success();
    }

    /**
    * 根据ID删除指定对象信息
    * @param id
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
            comboService.deleteById(id);
        return AjaxResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        return AjaxResult.success(comboService.selectById(id));
    }


    /**
    * 查看所有对象数据（不分页）
    */
    @GetMapping(value = "/list")
    public AjaxResult list(){
        return AjaxResult.success(comboService.selectList(null));
    }


    /**
    * 分页查询数据
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping(value = "/pagelist")
    public AjaxResult pageList(@RequestBody ComboQuery query)
    {
        Page<Combo> page = new Page<Combo>(query.getPage(),query.getPageSize());
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.eq("name","至尊版");
        page = comboService.selectPage(page,wrapper);
        return AjaxResult.success(new PageList<Combo>(page.getTotal(), page.getRecords()));
    }
}